/*
 * Copyright (c) 2011 Shanda Corporation. All rights reserved.
 *
 * Created on 2011-2-22.
 * 
 */

package com.whq.thrift.api.web.admin.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 分页模型.
 *
 * @author wanghuaiqiang
 */
public class PageData<T> {
    private int currentPage = 1;

    // 每页的记录条数
    private int pageSize = 20;

    //记录总数
    private int totalCount = 0;
    
    private int totalPage = 1;
    private List<T> resultList = new ArrayList<T>();
    
    //最多显示的页码个数
    private int maxDisplayPageCount = 5;
    
    // 可供选择的每页记录条数List
    private List<Integer> pageSizeOptionList = new ArrayList<Integer>(0);

    public PageData() {
    	pageSizeOptionList.add(10);
    	pageSizeOptionList.add(20);
    	pageSizeOptionList.add(50);
    }

    public PageData(int currentPage) {
        this.setCurrentPage(currentPage);
    }

    public PageData(int currentPage, int pageSize) {
        this.setCurrentPage(currentPage);
        this.setPageSize(pageSize);
    }

    /**
     * 从1开始
     *
     * @return
     */
    public final int getStartRow() {
        if (this.currentPage < 1) {
            this.currentPage = 1;
        }
        return (this.currentPage - 1) * this.pageSize + 1;
    }

    /**
     * currentPage * pageSize
     *
     * @return
     */
    public final int getEndRow() {
        if (this.currentPage < 1) {
            this.currentPage = 1;
        }
        int last = this.currentPage * this.pageSize;
        return this.totalCount > 0 && last > this.totalCount ? this.totalCount : last;
    }

    /**
     * 返回记录指针的开始位置
     *
     * @return
     */
    public final int getStartPos() {
        if (this.currentPage < 1) {
            this.currentPage = 1;
        }
        return (this.currentPage - 1) * this.pageSize;
    }

    /**
     * 最小为1
     *
     * @return the currentPage
     */
    public final int getCurrentPage() {
        return currentPage;
    }

    /**
     * 当前页只能大于1的值，默认为1
     *
     * @param currentPage the currentPage to set
     */
    public final void setCurrentPage(int currentPage) {
        if (currentPage < 1) {
            currentPage = 1;
        }
        this.currentPage = currentPage;
    }

    /**
     * @return the pageSize
     */
    public final int getPageSize() {
        return pageSize;
    }

    /**
     * pageSize只能为大于0的值，默认20
     *
     * @param pageSize the pageSize to set
     */
    public final void setPageSize(int pageSize) {
        if (pageSize < 0) {
            pageSize = 20;
        }
        this.pageSize = pageSize;
    }

    /**
     * 最小为0
     *
     * @return
     */
    public final int getTotalCount() {
        return totalCount;
    }

    /**
     * 设置总量，并计算总页数
     *
     * @param totalCount
     */
    public final void setTotalCount(int totalCount) {
        if (totalCount < 0) {
            totalCount = 0;
        }
        this.totalCount = totalCount;
        if (totalCount % pageSize == 0) {
            totalPage = totalCount / pageSize;
        } else {
            totalPage = totalCount / pageSize + 1;
        }
        if (currentPage > totalPage) {
            currentPage = totalPage;
        } else if (currentPage < 0) {
            currentPage = 1;
        }
    }

    /**
     * 最小为1
     *
     * @return
     */
    public int getTotalPage() {
        return totalPage;
    }

    public List<T> getResultList() {
        if (resultList == null) {
            return new ArrayList<T>();
        }
        return resultList;
    }

    public void setResultList(List<T> resultList) {
        this.resultList = resultList;
    }

    public int getMaxDisplayPageCount() {
        return maxDisplayPageCount;
    }

    public void setMaxDisplayPageCount(int maxDisplayPageCount) {
        this.maxDisplayPageCount = maxDisplayPageCount;
    }

    /**
     * 获取需要显示的页码列表
     *
     * @return
     */
    public List<Integer> getDisplayPageNums() {
        if (currentPage < 1) {
            currentPage = 1;
        }
        if (totalPage < 1) {
            totalPage = 1;
        }
        if (currentPage > totalPage) {
            currentPage = totalPage;
        }
        if (maxDisplayPageCount < 1) {
            maxDisplayPageCount = 5;
        }

        //显示的最前面的页码
        int beginPage = 1;
        //显示的最后面的页码
        int endPage = totalPage;

        if (maxDisplayPageCount < totalPage) {
            int halfDisplayCount = (maxDisplayPageCount - 1) / 2;

            beginPage = currentPage - halfDisplayCount;
            if (beginPage < 1) {
                beginPage = 1;
                endPage = beginPage + maxDisplayPageCount - 1;
            } else {
                endPage = currentPage + halfDisplayCount;
                if (endPage > totalPage) {
                    beginPage = totalPage - maxDisplayPageCount + 1;
                    endPage = totalPage;
                }
            }
        }

        List<Integer> ret = new ArrayList<Integer>();
        for (int i = beginPage; i <= endPage; i++) {
            ret.add(Integer.valueOf(i));
        }

        return ret;
    }

	public List<Integer> getPageSizeOptionList() {
		if (pageSizeOptionList == null) {
			pageSizeOptionList = new ArrayList<Integer>(0);
		}
		pageSizeOptionList.add(this.pageSize);
		Set<Integer> tmpSet = new HashSet<Integer>(pageSizeOptionList);
		pageSizeOptionList.clear();
		pageSizeOptionList.addAll(tmpSet);
		
		Collections.sort(pageSizeOptionList);
		return pageSizeOptionList;
	}

	public void setPageSizeOptionList(List<Integer> pageSizeOptionList) {
		this.pageSizeOptionList = pageSizeOptionList;
	}

}
